package com.china08.yxyapi.api.home.index.webgroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.sws.EdbWebGroupService;
import com.china08.yxyapi.util.ThumborUtil;
import com.squareup.pollexor.ThumborUrlBuilder;
import com.squareup.pollexor.ThumborUrlBuilder.ImageFormat;

@CrossOrigin
@RestController
public class HomeIndexWebGroupList1Controller extends YxyApiBaseController {

    @Autowired
    EdbWebGroupService edbWebGroupService;

    @RequestMapping(value = "/api_yw/home/index/webgroup/list1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        RespModel respModel = new RespModel();

        Integer bureauLevel = reqModel.bureauLevel;
        String areaIdProvince = reqModel.areaIdProvince;
        String areaIdCity = reqModel.areaIdCity;
        String areaIdDistrict = reqModel.areaIdDistrict;

        Map<String, Object> map = edbWebGroupService.HomeSchoolKongJianList(bureauLevel, 9, areaIdProvince, areaIdCity, areaIdDistrict);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        if (map != null) {
            list = (List<Map<String, Object>>) map.get("list");
        }

        List<Pojo> schoolList = new ArrayList<>();

        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            pojo.schoolNick = m.get("school_name") + "";
            pojo.address = m.get("school_name") + "";
            pojo.schoolWebUrl = "http://" + m.get("web_url") + "";

            List<String> schoolTypeList = new ArrayList<>();
            boolean bb0 = (boolean) m.get("is_st0");
            boolean bb1 = (boolean) m.get("is_st1");
            boolean bb2 = (boolean) m.get("is_st2");
            boolean bb3 = (boolean) m.get("is_st3");
            if (bb0) {
                schoolTypeList.add("幼儿园");
            }
            if (bb1) {
                schoolTypeList.add("小学");
            }
            if (bb2) {
                schoolTypeList.add("初中");
            }
            if (bb3) {
                schoolTypeList.add("高中");
            }
            pojo.schoolType = StringUtils.join(schoolTypeList.toArray(), ",");

            String schoolBadge = m.get("school_badge") + "";
            if (StringUtils.isBlank(schoolBadge) || "null".equals(schoolBadge)) {
                schoolBadge = "https://yxyosc.91yunxiao.com/yxyosc/2017/10/16/ca1ab5a0786881b2a231554bb3890925.png";
            }
            if (!schoolBadge.startsWith("http")) {
                schoolBadge = "https://yxyosc.91yunxiao.com/yxyosc/" + schoolBadge;
            }

            try {
                // 默认图片不做图片截取处理
                boolean b1 = schoolBadge.equals("https://yxyosc.91yunxiao.com/yxyosc/2017/10/16/ca1ab5a0786881b2a231554bb3890925.png");

                // 是否是gif图片
                boolean b2 = schoolBadge.endsWith("gif") || schoolBadge.endsWith("GIF");

                //
                if (!b1 && !b2) {
                    ThumborUrlBuilder tub = ThumborUtil.getThumborInstance().buildImage(schoolBadge);
                    tub.resize(60, 60);
                    tub.smart();
                    tub.trim();
                    tub.filter(ThumborUrlBuilder.quality(100));
                    if (schoolBadge.endsWith("jpg") || schoolBadge.endsWith("jpeg") || schoolBadge.endsWith("JPG") || schoolBadge.endsWith("JPEG")) {
                        tub.filter(ThumborUrlBuilder.format(ImageFormat.JPEG));
                    } else if (schoolBadge.endsWith("PNG") || schoolBadge.endsWith("png")) {
                        tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                    } else if (schoolBadge.endsWith("bmp") || schoolBadge.endsWith("BMP")) {
                        tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                    } else if (schoolBadge.endsWith("gif") || schoolBadge.endsWith("GIF")) {
                        tub.filter(ThumborUrlBuilder.format(ImageFormat.GIF));
                    }
                    schoolBadge = tub.toUrl();
                }
            } catch (Exception e) {
            }

            pojo.schoolBadge = schoolBadge;
            schoolList.add(pojo);
        }

        respModel.schoolList = schoolList;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public Integer bureauLevel;
        public String areaIdProvince;
        public String areaIdCity;
        public String areaIdDistrict;
    }

    public static class Pojo {
        public String schoolNick;
        public String address;
        public String schoolType;
        public String schoolBadge;
        public String schoolWebUrl;

    }

    public static class RespModel extends YxyApiRespModel {
        public List<Pojo> schoolList;
    }
}
